<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Packing Label</title>
    <style>
        @page {
            size: A4;
            margin: 10mm;
        }
        
        body {
            margin: 0;
            padding: 5mm;
            font-family: "Microsoft YaHei", "SimSun", Arial, sans-serif;
            font-size: 14px;
            line-height: 1.1;
        }
        
        .page-break {
            page-break-before: always;
            margin-top: 0;
        }
        
        .logo-section {
            text-align: center;
            margin-bottom: 3mm;
        }
        
        .logo {
            max-width: 25mm;
            max-height: 12mm;
        }
        
        .barcode-section {
            text-align: center;
            margin-bottom: 3mm;
        }
        
        .barcode-text {
            font-family: 'Courier New', monospace;
            font-size: 19px;
            font-weight: bold;
            letter-spacing: 1px;
            background-color: #f8f8f8;
            padding: 2mm;
            border: 1px solid #000;
            border-radius: 1mm;
        }
        
        .barcode-image {
            height: 15mm;
            max-width: 100%;
            border: 1px solid #ccc;
        }
        
        .barcode-label {
            font-size: 11px;
            color: #666;
            margin-top: 1mm;
        }
        
        .header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 3mm;
            border-bottom: 1px solid #000;
            padding-bottom: 1mm;
            font-size: 14px;
            font-weight: bold;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 2mm;
        }
        
        .items-table th,
        .items-table td {
            border: 1px solid #000;
            padding: 1mm;
            text-align: left;
            font-size: 14px;
            word-wrap: break-word;
            word-break: break-all;
            white-space: normal;
        }
        
        .items-table th {
            background-color: #f0f0f0;
            font-weight: bold;
        }
        
        .item-code { width: 18%; font-weight: bold; vertical-align: top; }
        .item-desc { width: 46%; vertical-align: top; line-height: 1.2; }
        .item-desc .sub-cn { font-size: 12px; color: #444; margin-top: 1mm; }
        .item-size { width: 12%; text-align: left; }
        .item-color { width: 12%; text-align: left; }
        .qty { width: 12%; text-align: center; vertical-align: top; }
    </style>
</head>
<body>
    
    {% macro render_barcode_section(material_request) %}
        <div class="barcode-section">
            <div style="font-size: 7px; margin-bottom: 1mm; color: #666;">Advanced Shipping Notice</div>
            <div class="barcode-text">{{ material_request or 'N/A' }}</div>
            <div style="margin-top: 2mm; text-align: center;">
                <img src="https://barcode.tec-it.com/barcode.ashx?data={{ material_request or 'N/A' }}&code=Code128&translate-esc=on&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=000000&bgcolor=FFFFFF&qunit=Mm&quiet=0"
                     alt="Barcode: {{ material_request or 'N/A' }}" 
                     class="barcode-image">
            </div>
        </div>
    {% endmacro %}

    {% macro render_logo_section() %}
        <div class="logo-section">
            <img src="/files/5295d4709d8305c1a3ac71a22341e98a.png" alt="System Logo" class="logo">
        </div>
    {% endmacro %}

    {% macro render_items_table(items, box_no) %}
        <table class="items-table">
            <thead>
                <tr>
                    <th class="item-code">Item Code</th>
                    <th class="item-desc">Description</th>
                    <th class="item-size">Size</th>
                    <th class="item-color">Color</th>
                    <th class="qty">Qty</th>
                </tr>
            </thead>
            <tbody>
                {% for item in items %}
                    {% if item.box_no == box_no %}
                    <tr>
                        <td class="item-code">{{ item.item or '' }}</td>
                        <td class="item-desc">
                            <div>{{ item.item_name or '' }}</div>
                            {% if item.item_name_cn %}<div class="sub-cn">{{ item.item_name_cn }}</div>{% endif %}
                        </td>
                        <td class="item-size">{{ item.item_size or '' }}</td>
                        <td class="item-color">{{ item.item_color or '' }}</td>
                        <td class="qty">{{ item.qty or 0 }}</td>
                    </tr>
                    {% endif %}
                {% endfor %}
            </tbody>
        </table>
    {% endmacro %}

    {% if doc.packing_item %}
        {% set box_numbers = [] %}
        {% for item in doc.packing_item %}
            {% if item.box_no not in box_numbers %}
                {% set _ = box_numbers.append(item.box_no) %}
            {% endif %}
        {% endfor %}
        
        {% for box_no in box_numbers %}
            {% if loop.index > 1 %}
                <div class="page-break"></div>
            {% endif %}
            
            {{ render_logo_section() }}
            {{ render_barcode_section(doc.material_request) }}
            
            <div class="header">
                <span>总箱数: {{ doc.total_boxes or 0 }}</span>
                <span>箱子编号: {{ box_no }}</span>
            </div>
            
            {{ render_items_table(doc.packing_item, box_no) }}
        {% endfor %}
    {% else %}
        {{ render_logo_section() }}
        {{ render_barcode_section(doc.material_request) }}
        
        <div class="header">
            <span>总箱数: {{ doc.total_boxes or 0 }}</span>
            <span>箱子编号: 1</span>
        </div>
        
        <table class="items-table">
            <thead>
                <tr>
                    <th class="item-code">Item Code</th>
                    <th class="item-desc">Item Description</th>
                    <th class="qty">Qty</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td colspan="3" style="text-align: center; color: #999;">No items found</td>
                </tr>
            </tbody>
        </table>
    {% endif %}
    
</body>
</html>